package com.bkhech.boot.sample.mvc.service.impl;

import com.bkhech.boot.sample.mvc.domain.UserInfo;
import com.bkhech.boot.sample.mvc.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author guowm
 * @date 2023/1/11
 */
@Component
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    final PasswordEncoder passwordEncoder;

    @Override
    public UserInfo loadUserByUsername(String username) throws UsernameNotFoundException {
        if (users.containsKey(username)) {
            return users.get(username);
        }
        throw new UsernameNotFoundException("用户不存在");
    }

    @Override
    public UserInfo getUserByUsername(String username) {
        return this.loadUserByUsername(username);
    }

    @Override
    public boolean isPasswordMatch(String rawPassword, String encodedPassword) {
        return passwordEncoder.matches(rawPassword, encodedPassword);
    }
}
